<template>
  <div ref="divRef" class="home-index" :style="style">
    <div class="pipeline-h pipeline has-watter" style="top: 455px;left: 0;height: 30px;width: 220px;"></div>
    <div class="pipeline pipeline-v has-watter" style="top: 300PX;left: 220px;height: 340px;"></div>
    <div class="pipeline pipeline-h has-watter" style="top: 300px;left: 236px;width: 40px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':settling[0] && settling[0].inletValve === 1}" style="top: 300px;left: 315px;width: 40px;"></div>
    <div class="pipeline pipeline-h has-watter" style="top: 624px;left: 236px;height: 16px;width: 40px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':settling[1] && settling[1].inletValve === 1}" style="top: 624px;left: 315px;height: 16px;width: 40px;"></div>


    <div class="pipeline pipeline-h" :class="{'has-watter':settling[0] && settling[0].inletValve === 1}" style="top: 300px;left: 556px;width: 40px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':settling[0] && settling[0].outletValve === 1}" style="top: 300px;left: 635px;width: 40px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':settling[1] && settling[1].inletValve === 1}" style="top: 624px;left: 556px;width: 40px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':settling[1] && settling[1].outletValve === 1}" style="top: 624px;left: 635px;width: 40px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':true}" style="top: 884px;left: 556px;width: 40px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':alarm[0] && alarm[0].ballStatus===1}" style="top: 884px;left: 635px;width: 40px;"></div>


    <div class="pipeline pipeline-v" :class="{'has-watter':settling[0] && settling[0].outletValve === 1}" style="top: 300px;left: 676px;height: 340px;"></div>
    <div class="pipeline pipeline-v" :class="{'has-watter':alarm[0] && alarm[0].ballStatus===1}" style="top: 624px;left: 676px;height: 276px;"></div>


    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut1}" style="top: 624px;left: 692px;width: 40px;"></div>
    <div class="pipeline pipeline-v" :class="{'has-watter':watterOut1}" style="top: 496px;left: 732px;height: 204px;"></div>

    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut1}" style="top: 496px;left: 748px;width: 40px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut1}" style="top: 684px;left: 748px;width: 16px;"></div>

    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut1 && pompe[0].pumpStatus === 1}" style="top: 479px;left: 836px;width: 20px;"></div>
    <div class="pipeline pipeline-v" :class="{'has-watter':watterOut1 && pompe[1].pumpStatus === 1}" style="top: 479px;left: 856px;height: 204px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut1 && pompe[1].pumpStatus === 1}" style="top: 667px;left: 839px;width: 16px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut2}" style="top: 479px;left: 856px;width: 52px;"></div>


    <div class="pipeline pipeline-v" :class="{'has-watter':watterOut2}"
         style="top: 206px;left: 908px;height: 570px"></div>

    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut2}" style="top: 206px;left: 924px;width: 23px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut2 && decay[0].inletValve===1}" style="top: 206px;left: 986px;width: 28px;"></div>

    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut2}" style="top: 479px;left: 924px;width: 23px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut2 && decay[1].inletValve===1}"  style="top: 479px;left: 986px;width: 28px;"></div>

    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut2}" style="top: 760px;left: 924px;width: 23px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut2 && decay[2].inletValve===1}" style="top: 760px;left: 986px;width: 28px;"></div>


    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut2 && decay[0].inletValve===1}"
         style="top: 206px;left: 1214px;width: 23px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut2 && decay[0].outletValve===1}"
         style="top: 206px;left: 1276px;width: 28px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut2 && decay[1].inletValve===1}"
         style="top: 479px;left: 1214px;width: 23px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut2 && decay[1].outletValve===1}"
         style="top: 479px;left: 1276px;width: 28px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut2 && decay[2].inletValve===1}"
         style="top: 760px;left: 1214px;width: 23px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut2 && decay[2].outletValve===1}"
         style="top: 760px;left: 1276px;width: 28px;"></div>


    <div class="pipeline pipeline-v" :class="{'has-watter':watterOut3}" style="top: 206px;left: 1304px;height: 570px"></div>

    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut3}" style="top: 479px;left: 1320px;width: 39px;"></div>

    <div class="pipeline pipeline-v" :class="{'has-watter':watterOut3}" style="top: 391px;left: 1359px;height: 204px"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut3}" style="top: 391px;left: 1375px;width: 40px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut3 && pompe[2].pumpStatus===1}" style="top: 374px;left: 1466px;width: 17px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut3 }" style="top: 579px;left: 1375px;width: 16px;"></div>
    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut3 && pompe[3].pumpStatus===1}" style="top: 562px;left: 1466px;width: 17px;"></div>
    <div class="pipeline pipeline-v" :class="{'has-watter':watterOut3 && (pompe[2].pumpStatus===1 || pompe[3].pumpStatus===1)}" style="top: 374px;left: 1483px;height: 204px"></div>

    <div class="pipeline pipeline-h" :class="{'has-watter':watterOut3 && (pompe[2].pumpStatus===1 || pompe[3].pumpStatus===1)}" style="top: 479px;left: 1483px;width: 177px;"></div>

    <!--沉淀池-->
    <pool-item v-for="(item,index) in settling" :key="`settling-${index}`"
               :style="{top:`${settlingP[index][0]}px`,left:`${settlingP[index][1]}px`}" v-bind="item"
               color="#387af6"></pool-item>

    <!--衰变池-->
    <pool-item v-for="(item,index) in decay" :key="`decay-${index}`"
               :style="{top:`${decayP[index][1]}px`,left:`${decayP[index][0]}px`}" v-bind="item"
               color="#9eb656"></pool-item>
    <!--    <pool-item style="top: 367px;left: 1014px;" color="#9eb656"></pool-item>-->
    <!--    <pool-item style="top: 648px;left: 1014px;" color="#9eb656"></pool-item>-->
    <!--应急池-->
    <pool-item v-for="(item,index) in emergency" :key="`emergency-${index}`"
               style="top: 809px;left: 356px;height: 150px;width: 200px;" v-bind="item"></pool-item>

    <!--泵-->
    <pompe-item :value="pompe[0]" :is-auto="isAuto" :left="764" :top="462"></pompe-item>
    <pompe-item :value="pompe[1]" :is-auto="isAuto" :left="764" :top="650"></pompe-item>
    <pompe-item :value="pompe[2]" :is-auto="isAuto" :left="1391" :top="357"></pompe-item>
    <pompe-item :value="pompe[3]" :is-auto="isAuto" :left="1391" :top="545"></pompe-item>

    <!--阀-->
    <valvole-item :value="settling[0]" :is-auto="isAuto" :position="1" :top="280" :left="276"></valvole-item>
    <valvole-item :value="settling[0]" :is-auto="isAuto" :position="2" :top="280" :left="596"></valvole-item>

    <valvole-item :value="settling[1]" :is-auto="isAuto" :position="1" :top="604" :left="278"></valvole-item>
    <valvole-item :value="settling[1]" :is-auto="isAuto" :position="2" :top="604" :left="596"></valvole-item>

    <valvole-item :value="decay[0]" :is-auto="isAuto" :position="1" :top="186" :left="947"></valvole-item>
    <valvole-item :value="decay[0]" :is-auto="isAuto" :position="2" :top="186" :left="1237"></valvole-item>

    <valvole-item :value="decay[1]" :is-auto="isAuto" :position="1" :top="459" :left="947"></valvole-item>
    <valvole-item :value="decay[1]" :is-auto="isAuto" :position="2" :top="459" :left="1237"></valvole-item>

    <valvole-item :value="decay[2]" :is-auto="isAuto" :position="1" :top="740" :left="947"></valvole-item>
    <valvole-item :value="decay[2]" :is-auto="isAuto" :position="2" :top="740" :left="1237"></valvole-item>

    <!--    <valvole-item :value="emergency[0]" :position="1" :top="864" :left="596"></valvole-item>-->


    <!--    <img class="image-item" style="top: 740px;left: 947px" src="../assets/home/阀门-开.png" alt="">-->
    <!--    <img class="image-item" style="top: 740px;left: 1237px" src="../assets/home/阀门-开.png" alt="">-->

    <!--    <img class="image-item" style="top: 604px;left: 276px" src="../assets/home/阀门-关.png" alt="">-->
    <!--    <img class="image-item" style="top: 604px;left: 596px" src="../assets/home/阀门-关.png" alt="">-->
    <!--    <img class="image-item" style="top: 864px;left: 596px" src="../assets/home/阀门-关.png" alt="">-->

    <img v-if="alarm[0]?alarm[0].ballStatus===1:false" class="image-item" style="top: 857px;left: 596px"
         src="../assets/home/泵2.png" alt="">
    <img v-else class="image-item" style="top: 857px;left: 596px" src="../assets/home/泵2-灰.png" alt="">

    <img class="image-item" style="top: 437px;left: 1545px;" alt="" src="../assets/home/流量计.png">


    <div class="buttons">
      <el-button type="primary" icon="el-icon-s-operation" @click="handleAuto">{{ modeText }}</el-button>
      <el-button type="primary" icon="el-icon-s-operation" @click="handleOperation">操作痕迹</el-button>
      <!--      <el-button type="primary" icon="el-icon-s-tools">排放</el-button>-->
    </div>

    <div class="text begin" style="top: 495px;left: 19px">
      <span>来自门诊或病房的放射性废水</span>
    </div>
    <div class="text end" style="top: 512px;left: 1523px">
      <span>排入医院公共污水管网</span>
    </div>
    <audio src="../assets/报警声音(1).mp3" ref="audio" muted></audio>
    <el-dialog title="操作痕迹" :visible.sync="visible" append-to-body>
      <el-table :data="operationList" height="80vh">
        <el-table-column label="设备名称" width="200px" prop="equName"></el-table-column>
        <el-table-column label="描述" prop="operation"></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import PoolItem from "@/views/PoolItem.vue";
import {getConfig, getOperation, setAuto} from "@/api/init";
import ValvoleItem from "@/views/ValvoleItem.vue";
import PompeItem from "@/views/PompeItem.vue";

export default {
  name: "Index",
  components: {PompeItem, ValvoleItem, PoolItem},
  data() {
    return {
      ws: null,
      settlingP: [[188, 356], [512, 356]],
      decayP: [[1014, 83], [1014, 367], [1014, 648]],
      list: [],
      autoMode: null,
      visible: false,
      operationList: [],
      xScale: 1,
      yScale: 1,
    };
  },
  computed: {
    settling: function () {
      return this.list.filter(item => item.equipmentType === 1)
    },
    decay: function () {
      return this.list.filter(item => item.equipmentType === 2)
    },
    emergency: function () {
      return this.list.filter(item => item.equipmentType === 5)
    },
    pompe: function () {
      return this.list.filter(item => item.equipmentType === 3)
    },
    alarm: function () {
      return this.list.filter(item => item.equipmentType === 4)
    },
    modeText: function () {
      return this.isAuto ? '自动模式' : "手动模式"
    },
    style: function () {
      return {
        transform: `scaleX(${this.xScale}) scaleY(${this.yScale})`
      }
    },
    isAuto: function () {
      return this.autoMode === 1
    },

    //沉淀池出水口
    watterOut1: function () {
      return this.settling.some(item => item.outletValve===1) || this.alarm[0]?.ballStatus===1
    },
    //泵出水口
    watterOut2: function () {
      return this.watterOut1 && (this.pompe[0]?.pumpStatus === 1 || this.pompe[1]?.pumpStatus === 1)
    },
    //衰变池出水口
    watterOut3: function () {
      return this.watterOut2 && this.decay.some(item => item.outletValve===1)
    }
  },
  methods: {
    onMessage: function ({data}) {
      const {data: result, type} = JSON.parse(data)
      if (type === 101) {
        return
      }
      if (type === 104) {
        return this.handleAlarm(result)
      }
      this.list = result
    },
    handleOperation: async function () {
      const res = await getOperation()
      console.log(res)
      this.operationList = res.data
      this.visible = true
    },
    handleAuto: async function () {
      this.$prompt('请输入密码', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({value}) => {
        return setAuto({
          configName: value,
          configValue: this.autoMode === 2 ? 1 : 2
        })
      }).then(({msg}) => {
        this.$message.success(msg)
        return this.loadMode()
      }).catch(() => {
      });

    },
    loadMode: function () {
      return getConfig().then(({data: {configValue}}) => {
        console.log(configValue)
        this.autoMode = parseInt(configValue)
      })
    },
    handleAlarm: function (data) {
      this.$msgbox.alert(data, '注意')
      this.$refs.audio.muted = false
      this.$refs.audio.play()
    },
    getScale: function () {
      const rect = this.$refs.divRef?.getBoundingClientRect()
      console.log(rect)
      this.xScale = rect.width / 1660
      this.yScale = rect.height / 1000
    },
    linkWs: function () {
      const url = `ws://${window.location.host}${process.env.VUE_APP_BASE_API}/webSocket/${this.$store.state.user.userId}`
      this.ws = new WebSocket(url)
      this.ws.onopen = function () {
        console.log('onOpen')
      }
      this.ws.onmessage = this.onMessage
      this.ws.onerror = () => {
        console.error('error')
        this.$notify({
          title: '连接断开，请刷新页面'
        })
        // this.ws.close()
        // this.linkWs()
      }
    }
  },
  mounted() {
    this.linkWs()

    this.loadMode()

    // setTimeout(()=>{
      this.getScale()
    // },500)

  }
};
</script>

<style scoped lang="scss">
.home-index {
  position: relative;
  height: 100vh;
  margin-top: -84px;
  transform-origin: top left;
}

.pipeline {
  position: absolute;
  height: 16px;
  background-image: linear-gradient(var(--deg), #d4dff3 0%, #eef5ff 50%, #d4dff3 100%),
  linear-gradient(#d7e1f3, #d7e1f3);
  background-blend-mode: normal, normal;
}

.pipeline-h {
  --deg: 0deg;
  height: 16px;
}

.pipeline-v {
  --deg: 90deg;
  width: 16px;
}

.buttons {
  position: absolute;
  top: 90px;
  left: 1496px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: stretch;

  & > .el-button, .el-dropdown {
    margin: 0 0 40px;
  }

  & > * {
    box-shadow: 0 0 15px 0 rgba(46, 112, 236, 0.3);
    border-radius: 10px;
    overflow: hidden;
    border: 1px solid #387af6 !important;
  }

  ::v-deep & .el-button {
    background-color: #ffffff;
    color: #387af6;
    border: none;
  }

}

.image-item, .text {
  position: absolute;
}

.begin {
  width: 182px;
  height: 12px;
  font-family: MicrosoftYaHei;
  font-size: 14px;
  font-weight: normal;
  font-stretch: normal;
  line-height: 60px;
  letter-spacing: 0;
  color: #387af6;
}

.end {
  width: 110px;
  height: 36px;
  font-size: 14px;
  font-weight: normal;
  font-stretch: normal;
  line-height: 24px;
  color: #7b8365;
}

.pipeline.has-watter {
  background-image: linear-gradient(var(--deg), #548ef8 0%, #67a0f1 50%, #548ef8 100%),
  linear-gradient(#1765f1, #1765f1);
}

</style>

